package com.ib.review.controller;

import com.ib.review.base.response.JsonResult;
import com.ib.review.pojo.dto.CompareParam;
import com.ib.review.pojo.dto.CompareRecordQuery;
import com.ib.review.pojo.dto.CompareResultQuery;
import com.ib.review.pojo.dto.CompareRoleParam;
import com.ib.review.pojo.po.CompareRecordPO;
import com.ib.review.pojo.po.CompareResultPO;
import com.ib.review.pojo.vo.CompareRecordVO;
import com.ib.review.pojo.vo.HeaderNode;
import com.ib.review.service.CompareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/review/compare")
public class CompareController {

    private final CompareService compareService;
    @Autowired
    public CompareController(CompareService compareService) {
        this.compareService = compareService;
    }

    @PostMapping("upload")
    public JsonResult<Void> upload(@RequestParam("file") MultipartFile file, CompareParam param) {
        compareService.upload(file,param);
        return JsonResult.ok();
    }
    @GetMapping("queryHeading")
    public JsonResult<Map<String,Map<String, List<HeaderNode>>>> queryHeading(CompareParam param){
        Map<String,Map<String, List<HeaderNode>>> map = compareService.queryHeading(param);
        return JsonResult.ok(map);
    }
    @PostMapping("addRole")
    public JsonResult<Void> addRole(@RequestBody CompareRoleParam param){
        compareService.addRole(param);
        return JsonResult.ok();
    }
    @GetMapping("queryResult")
    public JsonResult<List<CompareResultPO>> queryResult(CompareResultQuery compareResultQuery){
        List<CompareResultPO> list = compareService.queryResult(compareResultQuery);
        return JsonResult.ok(list);
    }
    @GetMapping("queryRecord")
    public JsonResult<List<CompareRecordPO>> queryRecord(CompareRecordQuery compareRecordQuery) {
        List<CompareRecordPO> list = compareService.queryRecord(compareRecordQuery);
        return JsonResult.ok(list);
    }
    @GetMapping("queryRecordById")
    public JsonResult<CompareRecordVO> queryRecordById(CompareRecordQuery compareRecordQuery) {
        CompareRecordVO vo = compareService.queryRecordById(compareRecordQuery);
        return JsonResult.ok(vo);
    }
    @PostMapping("exportToExcel")
    public ResponseEntity<byte[]> exportToExcel(@RequestBody Map<String, Object> requestData) {
        byte[] excelBytes = compareService.exportToExcel(requestData);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));

        headers.setContentDispositionFormData("attachment", "export-data" + timestamp + ".xlsx");

        return ResponseEntity.ok().headers(headers).body(excelBytes);
    }
}
